<?php

namespace app\admin\controller\daily;
use think\Db;
/**
 *
 * 订单
 *
 * @icon fa fa-circle-o
 */
class Riskoff
{
    /**
     *
     * 风控未支付订单15分钟内自动取消超过2单
     *
     * @icon fa fa-circle-o
     */
    public function index()
    {
//        $where['ontime'] = ['lt',time()-30*60];
        $where['status'] = 9;
        $list = DB::name('order')->where($where)->field('id,buyid,count(buyid) as num')->group('buyid')->select();
        if($list){
            foreach($list as $k=>$v){
                if ($v['num'] > 1) {
                    Db::name('personal')->where('id',$v['buyid'])->update(['switch'=>2]);
                }
            }
        }
    }



    /**
     *
     * 风控未支付订单30分钟内自动取消超过3单
     *
     * @icon fa fa-circle-o
     */
    public function risk()
    {
        $where['ontime'] = ['lt',time()-15*60];
        $where['status'] = 1;
        $list = DB::name('order')->where($where)->field('goods_id,id,num,buy_type')->select();
        // dump($list);exit;
        if($list){
            foreach($list as $k=>$v){
                $data['status'] = 9;
                $data['reason'] = '系统自动取消';
                DB::name('order')->where(['id'=>$v['id']])->update($data);
                Db::name('goods')->where('id',$v['goods_id'])->setInc('stock',$v['num']);
                if ($v['buy_type'] == 3) {
                    $ck['status'] = 0;
                    $ck['order_id'] = 0;
                    Db::name('goodsinfo')->where('order_id',$v['id'])->update($ck);
                }
            }
        }
    }


}
